package com.baby.workqueue;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer1 {

    public static void main(String[] args) throws IOException, TimeoutException {
        //1.连接工厂
        ConnectionFactory connectionFactory =new ConnectionFactory();
        //2.设置连接属性
        connectionFactory.setHost("192.168.247.132");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("baby");
        connectionFactory.setPassword("123456");
        connectionFactory.setVirtualHost("scc");
        //3.获取连接对象
        Connection connection = connectionFactory.newConnection();
        //4.获取通道
        Channel channel = connection.createChannel();

        //设置每次拿多少条信息（默认每次拿=消息/消费者个数）
        channel.basicQos(1);

        //5.获取队列
         Consumer consumer =new DefaultConsumer(channel){
             @Override
             public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                 try {
                     Thread.sleep(50);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }

                 System.out.println("消息的详细信息"+new String(body));
                 channel.basicAck(envelope.getDeliveryTag(),false);
             }
         };
        channel.basicConsume("work_queue",false,consumer);
    }

}
